﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace SystemCustomers.MessageUtils
{
    public class LogUtils
    {
        private static string sLog = "Application";
        private static string sAppName = "System Customers";
        /// <summary>
        /// Writes to log.
        /// </summary>
        /// <param name="LogString">The log string.</param>
        public static void WriteToLog(string LogString)
        {
            string AppPath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
            if (!AppPath.EndsWith("\\"))
            {
                AppPath = AppPath + "\\";
            }
            // create a writer and open the file
            System.IO.TextWriter tw = new System.IO.StreamWriter(AppPath + DateTime.Today.ToString("MMM dd yyyy") + " log.txt", true);
            // write a line of text to the file
            tw.WriteLine(DateTime.Now + ": " + LogString);
            // close the stream
            tw.Close();
        }

        /// <summary>
        /// Systems event logs Information.
        /// </summary>
        /// <param name="sEventLog">The s event log.</param>
        public static void SystemEventLogsInformation(string sEventLog)
        {
            if (!EventLog.SourceExists(sAppName))
                EventLog.CreateEventSource(sAppName, sLog);

            EventLog.WriteEntry(sAppName, sEventLog,
                EventLogEntryType.Information, 200);
        }

        /// <summary>
        /// Systems event logs Warning
        /// </summary>
        /// <param name="sEvent">The Message log</param>
        public static void SystemEventLogsWarning(string sEvent)
        {
            if (!EventLog.SourceExists(sAppName))
                EventLog.CreateEventSource(sAppName, sLog);

            EventLog.WriteEntry(sAppName, sEvent,
                EventLogEntryType.Warning, 211);
        }
        /// <summary>
        /// Systems event logs Error
        /// </summary>
        /// <param name="sEvent">The message log</param>
        public static void SystemEventLogsError(string sEvent)
        {
            if (!EventLog.SourceExists(sAppName))
                EventLog.CreateEventSource(sAppName, sLog);

            EventLog.WriteEntry(sAppName, sEvent,
                EventLogEntryType.Error, 222);
        }
    }
}
